package com.apptravel.util;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;


/**
 * 
 * @author jason
 *
 */
public class JsonUtil extends RenderUtil{
	private static final Logger log = Logger.getLogger(JsonUtil.class);
    
	//-- header �������� --//
	private static final String ENCODING_PREFIX = "encoding";
	private static final String NOCACHE_PREFIX = "no-cache";
	private static final String ENCODING_DEFAULT = "UTF-8";
	private static final boolean NOCACHE_DEFAULT = true;
	
	//-- content-type �������� --//
	private static final String TEXT_TYPE = "text/plain";
	private static final String JSON_TYPE = "text/json";
	private static final String XML_TYPE = "text/xml";
	private static final String HTML_TYPE = "text/html";
	private static final String JS_TYPE = "text/javascript";
	

	

	public static void renderJson(final Map map,  HttpServletResponse response, String dateFormat,final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(dateFormat));
		String jsonString = JSONObject.fromObject(map,jsonConfig).toString();
		log.debug("[ jsonString ]"+jsonString);
		render(JSON_TYPE, jsonString, response,headers);
	}
	

	public static String parseJson(final Map map){
		String jsonString = JSONObject.fromObject(map).toString();
		return jsonString;
	}



	public static void renderJson(final Map map,  HttpServletResponse response, final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		String jsonString = JSONObject.fromObject(map,jsonConfig).toString();
		log.debug("[ jsonString ]"+jsonString);
		render(JSON_TYPE, jsonString, response,headers);
	}
	

	public static void renderJson(final Object object,  HttpServletResponse response,String dateFormat,final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(dateFormat));
		String jsonString = JSONObject.fromObject(object,jsonConfig).toString();
		render(JSON_TYPE, jsonString, response,headers);
	}


	public static void renderJson(final Object object,  HttpServletResponse response,final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		String jsonString = JSONObject.fromObject(object,jsonConfig).toString();
		render(JSON_TYPE, jsonString, response,headers);
	}
	

	public static void renderJson(final Collection<?> collction,  HttpServletResponse response,String dateFormat,final String... headers) {
		/*
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true));*/
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(dateFormat));
		String jsonString = JSONArray.fromObject(collction,jsonConfig).toString();
		//System.out.println(jsonString);
		render(JSON_TYPE, jsonString, response,headers);
	}


	public static void renderJson(final Collection<?> collction,  HttpServletResponse response,final String... headers) {
		/*
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true));*/
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		String jsonString = JSONArray.fromObject(collction,jsonConfig).toString();
		//System.out.println(jsonString);
		render(JSON_TYPE, jsonString, response,headers);
	}
	

	public static void renderJson(final Object[] array,HttpServletResponse response, String dateFormat,final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor(dateFormat));
		String jsonString = JSONArray.fromObject(array,jsonConfig).toString();
		render(JSON_TYPE, jsonString, response,headers);
	}



	public static void renderJson(final Object[] array,HttpServletResponse response, final String... headers) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor());
		String jsonString = JSONArray.fromObject(array,jsonConfig).toString();
		render(JSON_TYPE, jsonString, response,headers);
	}

}
